Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

278
Visualizações
How to sort String from alphabet first then number (in javascript). example if the input is "asd5lk43fj6vc" and the output should be "acdfjklsv3456"

I'm working on a simple sorting program where i need to sort an input from a user to remove whitespace, special character and sort the string(from user input) ascendingly. im new to learning js and i found this code online.

let sortNumButton = document.getElementById('sortNumButton');
let sortOutputContainer = document.getElementById('sortOutputContainer');
let inputField = document.getElementById('inputField');

sortNumButton.addEventListener('click', function(){
let paragraph = document.createElement('p')

paragraph.innerText = inputField.value.split('').sort().join('').replace(/[^a-zA-Z0-9-. ]/g, "");
sortOutputContainer.appendChild(paragraph);
inputField.value = "";
})

the program works but the output prints the number first, not alphabet first

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can .replace the result afterwards, matching numeric characters and putting that capture group at the end instead of the beginning.

const inputValue = "asd5lk43fj6vc";
const sorted = [...inputValue.replace(/[^a-zA-Z0-9-. ]/g, '')].sort().join('');
const result = sorted.replace(/^(\d+)(.*)/, '$2$1');
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

You could check with isFinite for numbers and sort then the rest by their value.

const
    sort = string => string
        .split('')
        .sort((a, b) => isFinite(a) - isFinite(b) || a > b || -(a < b))
        .join('');

console.log(sort('asd5lk43fj6vc'));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda